package com.djl.sqtg.controller;


import com.djl.sqtg.entity.Coupon;
import com.djl.sqtg.entity.Goods;
import com.djl.sqtg.entity.LayuiResultData;
import com.djl.sqtg.service.ICouponService;
import com.djl.sqtg.service.IGoodsService;
import com.djl.sqtg.util.DateUtil;
import com.djl.sqtg.util.JsonResult;
import com.djl.sqtg.util.TencentCOSUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("coupon")
public class CouponController extends BaseController{
    @Autowired
    private ICouponService couponService;

    @RequestMapping({"getAllCoupon"})
    public LayuiResultData getAllCoupon(@RequestParam(required=false,defaultValue="1") int page,
                                        @RequestParam(required=false) int limit,
                                         String coupontype,
                                         String couponname
                                        ){
        System.out.println("coupontype:"+coupontype);
        System.out.println("couponname:"+couponname);
        List<Coupon> coupons;
        Integer count;
        LayuiResultData data = new LayuiResultData();
        if ((coupontype==null && couponname==null ) || (coupontype=="" && couponname=="" )){
            // 使用Pagehelper传入当前页数和页面显示条数会自动为我们的select语句加上limit查询
            // 从他的下一条sql开始分页
            PageHelper.startPage(page, limit);
            coupons=couponService.getAllCoupon();
            count=couponService.getCouponNum();
            data.setCount(count);
            data.setData(coupons);
        }else {
            coupons=couponService.getAllCouponBySearch( coupontype,   couponname);
            count=couponService.getCouponNumBySearch( coupontype,   couponname);
            data.setCount(count);
            data.setData(coupons);
        }
        return data;

    }
    @GetMapping({"updateCouponState/{cid}/{state}"})
    public JsonResult updateCouponState(@PathVariable Integer cid,@PathVariable Integer state){
        couponService.updateCouponState(cid,state);
        return new JsonResult(OK);
    }

    @PostMapping("addCoupon")
    public JsonResult addGoods(Coupon coupon,@RequestParam("file") MultipartFile file) {
        System.out.println("进入优惠券添加控制层");
        System.out.println("文件名："+file.getOriginalFilename());
        System.out.println("文件大小："+file.getSize());
        coupon.setCouponimg(TencentCOSUtil.UploadIMG(file,"coupon"));
        coupon.setCouponaddtime(DateUtil.getNowTime());
        System.out.println(coupon);
        couponService.addCoupon(coupon);
        return new JsonResult(OK);
    }

    @PostMapping("editCoupon")
    public JsonResult editCoupon(Coupon coupon,@RequestParam("file") MultipartFile file) {
        System.out.println("进入优惠券添加控制层");
        System.out.println("文件名："+file.getOriginalFilename());
        System.out.println("文件大小："+file.getSize());
        coupon.setCouponimg(TencentCOSUtil.UploadIMG(file,"coupon"));
        System.out.println(coupon);
        couponService.editCoupon(coupon);
        return new JsonResult(OK);
    }

    @GetMapping("delCoupon/{cid}")
    public JsonResult delCoupon(@PathVariable Integer cid) {
//        couponService.delCoupon(cid);
        return new JsonResult(OK);
    }
}
